#!/user/bin/env python3
# -*- coding: utf-8 -*-
from random import randint

from PIL import Image, ImageFont, ImageDraw, ImageFilter

# 1.创建一个空白图片
image1 = Image.new(mode='RGB',size=(200,100),color=(255,255,255))

# 2.渲染背景(每个尺寸都设置随机颜色)
pen = ImageDraw.Draw(image1)
for x in range(0,200):
    for y in range(0,100):
        pen.point((x,y),(randint(0,255),randint(0,255),randint(0,255)))

# 3.增加模糊滤镜
image1 = image1.filter(ImageFilter.BLUR)

# 4.渲染文字(增加滤镜后必须再次获取画笔才能写入文字)
pen = ImageDraw.Draw(image1)
font = ImageFont.truetype('../files/heheda.ttf',30)
# 生成四个数字
nums = (randint(0,9),randint(0,9),randint(0,9),randint(0,9))
# pen.text(xy=(50,30),text='hello',fill=(0,0,0),font=font)
for x in range(4):
    # 中文编码范围
    # text = chr(randint(0x4e00,0x9fa5))
    # 英文编码范围
    # text = chr(randint(0x41,0x5a))
    r = randint(0,255)
    g = randint(0,255)
    b = randint(0,255)
    pen.text(xy=(x*40,randint(10,40)),text=str(nums[x]),fill=(r,g,b),font=font)
image1.show()